<?php defined('SYSPATH') or die('No direct script access.');
/**
* Module Pendapatan
*/
class Module_accreport_reportinvoices {
	
	private $controller;
	private $ss;
	private $thesystems;
	private $mydatetime;
	private $myfunctions;
	private $officefunctions;
	private $message;
	private $display;
	
	static function &factory() {
	   $o = new Module_accreport_reportinvoices();
	  return $o;	
	}
	
	public function __construct()
	{
		$this->controller			 = Request::current()->controller();
		$this->ss              = Session::instance();
		$this->thesystems      = Helper_theSystems::factory();
		$this->mydatetime      = Helper_MyDateTime::factory();
		$this->myfunctions     = Helper_myFunctions::factory();
		$this->officefunctions = Helper_officefunctions::factory();
		$this->display 				 = Helper_displayitems::factory();
		$this->message         = Language_Message::factory();
	}
	
public function reportinvoices($tglawal, $tglakhir, $paid)
	{
		$check_multistore = $this->thesystems->system_auth('accounting_multistores');
		$storeid          =	$this->ss->get('storeid');

		$invoice_list = DB::select()
			->from('accinvoices');
			if($paid == '1') {
				$invoice_list = $invoice_list->where('paid','=',NULL);
			}
			if($paid == '2') {
				$invoice_list = $invoice_list->where('paid','=','1');
			}
			if(!is_null($check_multistore))
			{
				if($storeid != 1)
				{
					$invoice_list = $invoice_list->where('accstores_id','=',$storeid);
				}
			}
			if(!empty($tglawal)) {
				$invoice_list = $invoice_list->where(DB::expr('YEAR(datetime)'),'>=',$this->mydatetime->mktime_from_sql($tglawal,'Y'))
					->where(DB::expr('MONTH(datetime)'),'>=',$this->mydatetime->mktime_from_sql($tglawal,'m'))
					->where(DB::expr('DAY(datetime)'),'>=',$this->mydatetime->mktime_from_sql($tglawal,'d'));
			}
			if(!empty($tglakhir)) {
				$invoice_list = $invoice_list->where(DB::expr('YEAR(datetime)'),'<=',$this->mydatetime->mktime_from_sql($tglakhir,'Y'))
					->where(DB::expr('MONTH(datetime)'),'<=',$this->mydatetime->mktime_from_sql($tglakhir,'m'))
					->where(DB::expr('DAY(datetime)'),'<=',$this->mydatetime->mktime_from_sql($tglakhir,'d'));
			}
		$invoice_list  = $invoice_list->execute();
		$invoice_lists = $invoice_list->as_array();


		foreach($invoice_lists as $key=>$invoice_list) {
			$transaction = DB::select()
				->from('acctransactions')
				->where('accinvoices_id','=',$invoice_list['id'])
				->execute();
			$transaction_array = $transaction->as_array();
			$invoice_lists[$key]['transaction'] = $transaction_array;
		}
		
		$content['title']		= 'Report Invoices';
		$content['content']	= View::factory('report/reportinvoices')
			->set('invoice_lists',$invoice_lists)
			->set('tglawal',$tglawal)
			->set('tglakhir',$tglakhir)
			->render();
		
		return $content;
	}











}